package com.runfast.waimai.web.controller.Driver_controller;

import com.runfast.paotui.dao.model.RunfastCuseraddress;
import com.runfast.paotui.dao.model.RunfastLoginRecord;
import com.runfast.paotui.dao.model.RunfastShopper;
import com.runfast.paotui.service.RunfastCuseraddressService;

import com.runfast.waimai.dao.model.RunfastGoodsSellChildren;
import com.runfast.waimai.dao.model.RunfastGoodsSellRecord;


import com.runfast.waimai.service.RunfastGoodsSellChildrenService;
import com.runfast.waimai.service.RunfastGoodsSellRecordService;
import com.runfast.waimai.util.GPSUtil;
import com.runfast.waimai.util.MyUtil;
import com.runfast.waimai.util.PageBean;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.io.UnsupportedEncodingException;
import java.util.List;
import java.util.Map;


@Controller
@RequestMapping("/driver/task/*")
public class DrivertaskController extends DriverBaseController {
    @Resource
    private RunfastGoodsSellRecordService goodsSellRecordService;
    //外卖商品订单RunfastGoodsSellRecord
    @Resource
    private RunfastGoodsSellChildrenService goodsSellRecordChildrenService;
    //runfast_goods_sell_children 外卖商品订单记录
    @Resource
    private RunfastCuseraddressService cuserAddressService;
    
	@RequestMapping(value="index",method=RequestMethod.POST)//测试时要注意骑手是不登陸的
	public @ResponseBody ModelMap index(@RequestParam(value = "page",required = false,defaultValue = "0") Integer page,

										@RequestParam(value = "pageSize",required = false,defaultValue = "10")Integer pageSize,String token){
		Integer page1=page*pageSize;
		ModelMap model = new ModelMap();
		RunfastShopper shopper = super.getLoginShopper(token);
		if(shopper!=null){//
			// 外卖商品订单

			List<RunfastGoodsSellRecord> list=goodsSellRecordService.findListPageBean( pageSize, page1, 8, shopper.getId(),null,null);
			int count=goodsSellRecordService.findCount( pageSize, page, 8, shopper.getId(),null,null);
			PageBean<RunfastGoodsSellRecord> pages=new PageBean<RunfastGoodsSellRecord>();
			pages.setData(list);//当前页面数据
			pages.setTotalPage(count/pageSize);//总页数,每页面显示10条数据
			pages.setCurrentPage(page);/// 当前页，要协商
			pages.setAllRow(count);//总记录数
	//		PageBean<RunfastGoodsSellRecord> pages  = goodsSellRecordService.getPageBean4Driver(rows, page, 8, shopper.getId(),null,null);
	  if(page<=pages.getTotalPage()){
	  model.put("CurrentPage", pages.getCurrentPage());//// 当前页,要和前端协商，如果不需要的话，就不用商量了
	  model.put("totalPage", pages.getTotalPage());//总页数
	  model.put("sell", pages.getData());//// 当前页数据
//	  model.put("totalsell", goodsSellRecordService.totalbyshopperid(shopper.getId()));
	  model.put("totalsell", pages.getAllRow());//// 总记录数
	  }else{
		  model.put("CurrentPage", pages.getCurrentPage());//当前页面
		  model.put("totalPage", pages.getTotalPage());//总页数
		  model.put("sell", "");//当前页数据
//		  model.put("totalsell", goodsSellRecordService.totalbyshopperid(shopper.getId()));
		  model.put("totalsell", pages.getAllRow());//总记录数
	  }
		}else {
			model.put("msg","骑手不能为空");
		}
		return model;
 } 

	@RequestMapping(value="detail",method=RequestMethod.POST)//高德坐标转换
	public @ResponseBody ModelMap detail(@RequestParam("id")Integer id ) throws UnsupportedEncodingException{
		//,@RequestParam("version")Integer version
		       ModelMap model = new ModelMap();
		       if(id!=null){
		   RunfastGoodsSellRecord goodsSellRecord  = goodsSellRecordService.get(id);
		   //// 外卖商品订单
		   if(goodsSellRecord!=null){
			   
			 //  GoodsSellRecord gr =goodsSellRecordService.findByOrderCode(orderCode); ///订单信息
//				if(version!=null){
//					if(version==1){
//						double[] businessAddress = GPSUtil.bd09_To_Gcj02(Double.valueOf(goodsSellRecord.getBusinessaddresslat()), Double.valueOf(goodsSellRecord.getBusinessaddresslng()));
//						com.runfast.paotui.dao.model.RunfastCuseraddress cuseradd =  cuserAddressService.findById(goodsSellRecord.getUseraddressid());
//						double[] userAddress = GPSUtil.bd09_To_Gcj02(Double.valueOf(cuseradd.getLatitude()), Double.valueOf(cuseradd.getLongitude()));
//
//						goodsSellRecord.setUseraddresslat(String.valueOf(userAddress[0]));
//						goodsSellRecord.setUseraddresslng(String.valueOf(userAddress[1]));
//
//						goodsSellRecord.setBusinessaddresslat(String.valueOf(businessAddress[0]));
//						goodsSellRecord.setBusinessaddresslng(String.valueOf(businessAddress[1]));
//
//
//					}else{
						Map<String,Object> loc = MyUtil.getGDlocFromBD( goodsSellRecord.getBusinessaddresslng(), goodsSellRecord.getBusinessaddresslat()) ;
						Map<String,Object> userLoc = MyUtil.getLocFromAddr(goodsSellRecord.getUseraddress() + goodsSellRecord.getAddress());
						//System.out.println("addr = "+ gr.getUserAddress() + gr.getAddress() );
						if(userLoc != null){

							goodsSellRecord.setUseraddresslat(userLoc.get("lat").toString());
							goodsSellRecord.setUseraddresslng(userLoc.get("lng").toString());
							//System.out.println("设置好坐标lat" + gr.getBusinessAddressLat() +"lng = " + gr.getBusinessAddressLng());

						}

						if(loc != null){

							goodsSellRecord.setBusinessaddresslat(loc.get("lat").toString());
							goodsSellRecord.setBusinessaddresslng(loc.get("lng").toString());
							//System.out.println("设置好坐标lat" + gr.getBusinessAddressLat() +"lng = " + gr.getBusinessAddressLng());

						}

//						else{
//							//LOG.info(""百度坐标转高德坐标失败"");
//						}
//					}
//				}


			   //把商家的坐标转换成高德的坐标
				
			   
			         List<RunfastGoodsSellChildren> list   =
			       goodsSellRecordChildrenService.findbyorderid(goodsSellRecord.getId());
		      if(list!=null&&list.size()>0){
		    	  goodsSellRecord.setGoodsSellRecordChildren(list);
		        }
		         if(goodsSellRecord.getUseraddressid()!=null){
		     RunfastCuseraddress cuseradd =  cuserAddressService.findById(goodsSellRecord.getUseraddressid());
		        
		         }
		      }
		   
		         model.put("sell",goodsSellRecord);
		   
		   } 
		       return model;
		}
}
	

